//
//  ALVistorView.swift
//  ALSinaWeiBoSwift
//
//  Created by 柴瑞军 on 16/4/8.
//  Copyright © 2016年 myCompany. All rights reserved.
//

import UIKit
//设置代理
protocol ALVistorViewDeledgate:NSObjectProtocol{
//协议中的方法:swift语法中默认会将第一个外部参数进行省略,所以再次要同时定义外部参数和内部参数,把自己传出去
    func visitorViewDidClickRegisterButton()
    func visitorViewDidClickloginButton()

}
class ALVistorView: UIView {

//    定义代理属性
    weak var deledgate:ALVistorViewDeledgate?
    
//给自定义的view上添加子控件
   override init(frame: CGRect) {
        super.init(frame: frame)
        
//        增加子控件
    PreparaUI()
    
    backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    外界访问用来设置控制器界面的视图
    func setControllerView(imageNamed:String,title:String)  {
        messageLabel.text = title
        iconView.image = UIImage.init(named: imageNamed)
        iconView.layer.removeAnimationForKey("nn")
        coverView.hidden = true
        homeView.hidden = true
    }
    
    private func PreparaUI(){
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton!)
        addSubview(loginBtn!)
//        禁用属性
        coverView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints  = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton?.translatesAutoresizingMaskIntoConstraints = false
        loginBtn?.translatesAutoresizingMaskIntoConstraints = false
//        设置约束
//    旋转背景的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
//       home的约束

        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
//        label的约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
//        遮盖的约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
        
////        注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerButton!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0 ))
        addConstraint(NSLayoutConstraint(item: registerButton!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
//
//        登录的约束
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
    }
    
//    MARK:-懒加载
//    转轮
    private lazy var  iconView:UIImageView = {
     let iiconV = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
//        设置动画
    let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI*2
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        // 动画完成不要移除动画
        animation.removedOnCompletion = false
        iiconV.layer.addAnimation(animation, forKey: "nn")
        
        return iiconV
    }()
//    遮盖
    private lazy var coverView:UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
//    小房子
    private lazy var homeView:UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_house"))
//    消息
    private lazy var messageLabel:UILabel = {
        let messageLabel = UILabel()
        
        messageLabel.text = "冯长贱是猴子搬来的救兵,你知道吗"
        messageLabel.textColor = UIColor.blackColor()
        messageLabel.textAlignment = NSTextAlignment.Center
        messageLabel.font = UIFont.systemFontOfSize(18)
        messageLabel.numberOfLines = 0
        
    return messageLabel
    }()
    
//    注册按钮
    private lazy var registerButton:UIButton? = {
    
    let registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        //设置背景
        registerBtn.setBackgroundImage(UIImage.init(named:"common_button_white_disable"), forState: UIControlState.Normal)
        //        设置字体颜色
        registerBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //       监听点击的方法
        registerBtn.addTarget(self, action: #selector(ALVistorView.registerBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return registerBtn
    }()
    
//    登录按钮
    private lazy var loginBtn:UIButton? = {
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        //设置背景
        loginBtn.setBackgroundImage(UIImage.init(named:"common_button_white_disable"), forState: UIControlState.Normal)
        //        设置字体颜色
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        
//       监听点击的方法
        loginBtn.addTarget(self, action: #selector(ALVistorView.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return loginBtn

    }()
    
    
    
    //MARK:-点击登录按钮后要执行的方法
    func loginBtnClick(){
    deledgate?.visitorViewDidClickloginButton()
    }
    //MARK:-点击注册按钮后调用的方法
    func registerBtnClick(){
    deledgate?.visitorViewDidClickRegisterButton()
    }
}
